#!/bin/bash

MODULE_LIST=`find . -name '.gitmodules'`

for GITM in $MODULE_LIST; do
    PARENT_PATH=$(dirname $GITM)
    pushd $PARENT_PATH
    for LINE in `git config -f .gitmodules --list | grep "\.url=https://github.com"`; do
        echo "ORIGIN: $LINE"
        MOD_PATH=$(echo $LINE | sed "s|^submodule\.\([^.]*\)\.url.*$|\1|")
        MOD_URL=$(echo $LINE | sed 's|^.*\.url=||g;s|\.git$||g')
        NEW_URL=$(echo $MOD_URL | sed 's|github.com/.*/|gitcode.com/grpc-mirror/|g')
        # echo $MOD_PATH,$MOD_URL,$NEW_URL
        echo ">>>>>>> git submodule set-url $MOD_PATH $NEW_URL"
        git submodule set-url $MOD_PATH $NEW_URL
    done
    popd
done
